package com.zjd.association.controller;

import cn.hutool.core.util.StrUtil;
import com.zjd.association.common.PageWrapper;
import com.zjd.association.common.R;
import com.zjd.association.entity.Notices;
import com.zjd.association.entity.Users;
import com.zjd.association.handle.CacheHandle;
import com.zjd.association.service.NoticesService;
import com.zjd.association.service.UsersService;
import com.zjd.association.utils.DateUtils;
import com.zjd.association.utils.UUIDUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 通知记录 控制层
 *
 * @author: zjd
 * @date: 2024-5-22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/notices")
public class NoticesController {

    private final CacheHandle cacheHandle;

    private final NoticesService noticesService;

    private final UsersService usersService;

    /**
     * 获取路径
     *
     * @return 路径
     */
    @RequestMapping("")
    public String index() {
        return "pages/Notices";
    }

    /**
     * 获取通知记录
     *
     * @param id 通知记录id
     * @return 通知记录
     */
    @GetMapping("/info")
    public R getInfo(String id) {
        Notices notices = noticesService.getOne(id);
        return R.ok(notices);
    }

    /**
     * 获取通知记录 分页
     *
     * @param pageIndex 分页页数
     * @param pageSize  分页大小
     * @param token     用户token
     * @param title     标题
     * @param teamName  活动名称
     * @return 通知记录
     */
    @GetMapping("/page")
    public R getPageInfos(
            Long pageIndex, Long pageSize,
            String token, String title, String teamName
    ) {
        //获取用户记录
        Users user = usersService.getOne(cacheHandle.getUserInfoCache(token));
        if (user.getType() == 0) {
            //管理员查询全部
            PageWrapper page = noticesService.getPageAll(pageIndex, pageSize, title, teamName);
            return R.ok(page);
        } else {
            PageWrapper page = noticesService.getPageById(pageIndex, pageSize, user.getId(), title, teamName);
            return R.ok(page);
        }
    }

    /**
     * 添加通知记录
     *
     * @param notices 通知记录信息
     * @return 结果
     */
    @PostMapping("/add")
    public R addInfo(Notices notices) {
        notices.setId(UUIDUtils.makeIDByCurrent());
        notices.setCreateTime(DateUtils.getNowDate("yyyy-MM-dd"));
        if (StrUtil.isBlank(notices.getTeamId())) {
            notices.setTeamId(null);
        }
        //添加
        noticesService.add(notices);
        return R.ok();
    }

    /**
     * 更新通知记录
     *
     * @param notices 通知信息
     * @return 结果
     */
    @PostMapping("/upd")
    public R updInfo(Notices notices) {
        noticesService.update(notices);
        return R.ok();
    }

    /**
     * 删除通知记录
     *
     * @param id 通知记录id
     * @return 结果
     */
    @PostMapping("/del")
    public R delInfo(String id) {
        Notices notices = noticesService.getOne(id);
        noticesService.delete(notices);
        return R.ok();
    }

}